package com.gusto.samples.wine;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gusto.engine.repository.service.RepositoryService;
import com.gusto.engine.semsim.exceptions.ConfigException;
import com.gusto.engine.beans.exceptions.ResCreationException;
import com.gusto.engine.semsim.exceptions.SimilarityException;
import com.gusto.engine.beans.model.Res;
import com.gusto.engine.semsim.service.SimilarityService;
import com.gusto.utils.cli.SemSimCLI;
import com.gusto.utils.semantic.SemanticService;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.rdf.model.Resource;

public class TestWine2 extends SemSimCLI {
	
	private static Logger log = Logger.getLogger(TestWine2.class);
	
	private String baseURI = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#";
	
	private static ApplicationContext factory = new ClassPathXmlApplicationContext("config/wine/wine-semsim-*.xml");
	private static SimilarityService similarityService = (SimilarityService)factory.getBean("similarityService");
	private static SemanticService semanticService = (SemanticService)factory.getBean("semanticService");
	private static RepositoryService repositoryService = (RepositoryService)factory.getBean("repositoryService");
	
	public TestWine2(SimilarityService semanticSimilarity, RepositoryService modelService, SemanticService semanticService) {
		super(semanticSimilarity, modelService, semanticService);
	}
	
	public static void main(String[] args) throws SimilarityException, ResCreationException, SQLException, IOException, ConfigException {
		TestWine2 testWine2 = new TestWine2(similarityService, repositoryService, semanticService);
		testWine2.execute(args);
		
	}
	
	public Double getWineSimilarity(String wine1, String wine2) throws SimilarityException, ResCreationException, ResCreationException {
		long start = System.currentTimeMillis();
		Res res1 = repositoryService.getResource(baseURI + "" + wine1);
		Res res2 = repositoryService.getResource(baseURI + "" + wine2);
		
		
		System.out.println(res1);
		System.out.println(res2);
		
		Double sim = similarityService.getSimilarity("wine", res1, res2);
		log.info("Dist. Wine " + (System.currentTimeMillis() - start) + " (" + wine1 + "-" + wine2 + ") " + sim);
		return sim;
	}
	
	public void load(String file) {
		//ssService.loadOntologyFromFile("wine", file);
		semanticService.loadDataFromFile("wine", file, true);
	}
	
	public void resource(String resource) {
		semanticService.getResourceProperties("http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#" + resource, null);
	}
	
	public void similarity(String res1, String res2) throws SimilarityException, ResCreationException {
		System.out.println(this.getWineSimilarity(res1, res2));
	}
	
	public void neighborhood(String resource) throws SimilarityException, ResCreationException {
		List<QuerySolution> sols = semanticService.sparqlQuery("data/wine/wineList.rq");
		List<String> ordered = new ArrayList<String>();
		for (QuerySolution sol : sols) {
			Resource res = sol.getResource("wine");
			String wine = res.getURI().substring( res.getURI().lastIndexOf("#")+1 );
			Double sim = this.getWineSimilarity(resource, wine);
			ordered.add((sim * 100) + " (" + wine + ")");
		}
		Collections.sort(ordered);
		Collections.reverse(ordered);
		for (String o : ordered) {
			System.out.println(o);
		}
	}
	
	public void help() {
		System.out.println("---------------------");
		System.out.println("Help");
		System.out.println("---------------------");
		System.out.println("load data/wine.rdf");
		System.out.println("resource MariettaZinfandel");
		System.out.println("resource Bancroft");
		System.out.println("query data/wineryList.rq");
		System.out.println("similarity MariettaZinfandel CotturiZinfandel");
		System.out.println("similarity MariettaZinfandel FormanCabernetSauvignon");
		System.out.println("similarity MariettaZinfandel MariettaZinfandel");
		System.out.println("neighborhood FormanChardonnay");
	}
	
}
